﻿using System;
using System.IO;

namespace Projky.Common {
    class ScriptFolder {
        public ScriptFolder(string directoryPath, string barName) {
            DirectoryPath = directoryPath;
            BarName = barName;
        }

        public Script[] GetScripts() {
            if (Directory.Exists(DirectoryPath) == false) {
                return new Script[0];
            }

            string[] files = Directory.GetFiles(DirectoryPath, "*.sql");
            
            Script[] scripts = new Script[files.Length];

            for (int i = 0; i < files.Length; i++) {
                string filePath = files[i];
                string displayName = Path.GetFileNameWithoutExtension(filePath);
                string commandName = "Projky" + BarName + displayName;
                commandName = ReplaceIllageChar(commandName);
                string content = ScriptIOHelper.ReadFile(filePath);
                scripts[i] = new Script(filePath, displayName, commandName, content);
            }

            return scripts;
        }

        string ReplaceIllageChar(string commandname) {
            return commandname.Replace('.', '_').Replace('~', '_').Replace('`', '_')
                    .Replace('@', '_').Replace('#', '_').Replace('$', '_')
                    .Replace(',', '_').Replace(' ', '_').Replace('>', '_')
                    .Replace('^', '_').Replace('&', '_').Replace('*', '_')
                    .Replace('{', '_').Replace(']', '_').Replace(';', '_')
                    .Replace('(', '_').Replace(')', '_').Replace('-', '_')
                    .Replace('+', '_').Replace('=', '_').Replace('[', '_')
                    .Replace(':', '_').Replace('\\', '_').Replace('|', '_')
                    .Replace('?', '_').Replace('<', '_').Replace('>', '_');
            ;
        }

        internal string DirectoryPath;
        internal string BarName;
    }
}
